/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    cpp-ethereum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CommonData.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/

// Modified by Evgeniy Sukhomlinov 2018

#include "CommonData.h"
#include <cstdlib>
#include "Exceptions.h"

using namespace XDag;

int XDag::FromHex(char i, WhenError _throw)
{
    if(i >= '0' && i <= '9')
    {
        return i - '0';
    }
    if(i >= 'a' && i <= 'f')
    {
        return i - 'a' + 10;
    }
    if(i >= 'A' && i <= 'F')
    {
        return i - 'A' + 10;
    }
    if(_throw == WhenError::Throw)
    {
        BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(i));
    }
    else
    {
        return -1;
    }
}

bool XDag::SetEnv(const char name[], const char value[], bool override)
{
#ifdef _WIN32
    if(!override && std::getenv(name) != nullptr)
        return true;

    return ::_putenv_s(name, value) == 0;
#else
    return ::setenv(name, value, override ? 1 : 0) == 0;
#endif
}
